/*
 * Copyright (c) 2012-2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP. 
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed for CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.common.message;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * <b>exception fremework, descriptor de mensajes customizados base</b>
 *
 * @author  W.
 * @version 1.0, Date 08/03/2012
 * @since 1.0
 */
public class RootMessageDescriptor implements Serializable {

	/**
	 * default serial version
	 */
	private static final long serialVersionUID = 1L;

    /** */
	private String messageKey;
	/** */
	private String technicalMessage;
    /** */
	private String endUserMessage;
	/** */
	private String baseResourceBundle;
	
	/**
	 * default constructor
	 */
	public RootMessageDescriptor() {
		this.messageKey = null;
		this.technicalMessage = null;
		this.endUserMessage = null;
		this.baseResourceBundle = null;
	}

	/**
	 *
	 */
	protected RootMessageDescriptor(String messageKey, String baseResourceBundle) {
		this.messageKey = messageKey;
		this.baseResourceBundle = baseResourceBundle;
	}


	private String formatMessage(String message, Object [] params) {
		MessageFormat fmt = new MessageFormat(message);
		return fmt.format(params);
	}

   protected static ClassLoader getCurrentLoader(Object fallbackClass) {
       ClassLoader loader =
           Thread.currentThread().getContextClassLoader();
       if (loader == null) {
           loader = fallbackClass.getClass().getClassLoader();
       }
       return loader;
   }

   /**
	 *
	 * @return
	 */
	public final String getTechnicalMessage() {
		return technicalMessage;
	}

	/**
	 *
	 * @param params
	 * @return
	 */
	public final String getTechnicalMessage(Object [] params) {
		ResourceBundle rb = ResourceBundle.getBundle(getBaseResourceBundle(),
				Locale.getDefault(), getCurrentLoader(getTechnicalMessage()));
		technicalMessage = rb.getString(getMessageKey());
		technicalMessage = formatMessage(getTechnicalMessage(), params);
		return formatMessage(getTechnicalMessage(), params);
	}

	/**
	 *
	 * @return
	 */
	public final String getEndUserMessage() {
		return endUserMessage;
	}

	/**
	 *
	 * @param params
	 * @return
	 */
	public final String getEndUserMessage(Object [] params) {
		ResourceBundle rb = ResourceBundle.getBundle(getBaseResourceBundle(),
				Locale.getDefault(), getCurrentLoader(getEndUserMessage()));
		endUserMessage = rb.getString(getMessageKey()+MessageDescriptorConstant.UM);
		endUserMessage = formatMessage(getEndUserMessage(), params);
		return formatMessage(endUserMessage, params);
	}

	/**
	 * overrides toString() method
	 */
	public final String toString(){
		return ("technicalMessage : "
		+ getTechnicalMessage()
		+ "endUserMessage : "
		+ getEndUserMessage());
	}

	/**
	 * @return the baseResourceBundle
	 */
	public final String getBaseResourceBundle() {
		return this.baseResourceBundle;
	}

	/**
	 * @param baseResourceBundle the baseResourceBundle to set
	 */
	public final void setBaseResourceBundle(String baseResourceBundle) {
		this.baseResourceBundle = baseResourceBundle;
	}

   /**
	 * @return the messageKey
	 */
	public final String getMessageKey() {
		return messageKey;
	}

	/**
	 * @param messageKey the messageKey to set
	 */
	public final void setMessageKey(String messageKey) {
		this.messageKey = messageKey;
	}

}

